package cn.iocoder.yudao.module.ride.controller.admin.bank;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.bank.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.bank.BankDO;
import cn.iocoder.yudao.module.ride.service.bank.BankService;

@Tag(name = "管理后台 - 司机银行卡")
@RestController
@RequestMapping("/ride/bank")
@Validated
public class BankController {

    @Resource
    private BankService bankService;

    //创建司机银行卡
    @PostMapping("/create")
    @Operation(summary = "创建司机银行卡")
    @PreAuthorize("@ss.hasPermission('ride:bank:create')")
    public CommonResult<Long> createBank(@Valid @RequestBody BankSaveReqVO createReqVO) {
        return success(bankService.createBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新司机银行卡")
    @PreAuthorize("@ss.hasPermission('ride:bank:update')")
    public CommonResult<Boolean> updateBank(@Valid @RequestBody BankSaveReqVO updateReqVO) {
        bankService.updateBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除司机银行卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:bank:delete')")
    public CommonResult<Boolean> deleteBank(@RequestParam("id") Long id) {
        bankService.deleteBank(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除司机银行卡")
                @PreAuthorize("@ss.hasPermission('ride:bank:delete')")
    public CommonResult<Boolean> deleteBankList(@RequestParam("ids") List<Long> ids) {
        bankService.deleteBankListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得司机银行卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:bank:query')")
    public CommonResult<BankRespVO> getBank(@RequestParam("id") Long id) {
        BankDO bank = bankService.getBank(id);
        return success(BeanUtils.toBean(bank, BankRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得司机银行卡分页")
    @PreAuthorize("@ss.hasPermission('ride:bank:query')")
    public CommonResult<PageResult<BankRespVO>> getBankPage(@Valid BankPageReqVO pageReqVO) {
        PageResult<BankDO> pageResult = bankService.getBankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BankRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出司机银行卡 Excel")
    @PreAuthorize("@ss.hasPermission('ride:bank:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBankExcel(@Valid BankPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BankDO> list = bankService.getBankPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "司机银行卡.xls", "数据", BankRespVO.class,
                        BeanUtils.toBean(list, BankRespVO.class));
    }

}